@echo off
echo ========================================
echo 启动Chrome调试模式（无谷歌账号版本）
echo ========================================

REM 关闭所有Chrome进程
echo 正在关闭所有Chrome进程...
taskkill /f /im chrome.exe 2>nul
timeout /t 2 /nobreak >nul

REM 创建Chrome调试配置文件目录
echo 创建Chrome调试配置文件目录...
if not exist "D:\anr-analyzer\chrome-debug-profile" (
    mkdir "D:\anr-analyzer\chrome-debug-profile"
    echo 已创建目录: D:\anr-analyzer\chrome-debug-profile
) else (
    echo 目录已存在: D:\anr-analyzer\chrome-debug-profile
)

REM 查找Chrome安装路径
echo 查找Chrome安装路径...
set CHROME_PATH=""
if exist "C:\Program Files\Google\Chrome\Application\chrome.exe" (
    set CHROME_PATH="C:\Program Files\Google\Chrome\Application\chrome.exe"
    echo 找到Chrome: %CHROME_PATH%
) else if exist "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (
    set CHROME_PATH="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    echo 找到Chrome: %CHROME_PATH%
) else (
    echo ❌ 未找到Chrome安装路径，请手动设置CHROME_PATH变量
    echo 常见路径：
    echo   - C:\Program Files\Google\Chrome\Application\chrome.exe
    echo   - C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
    pause
    exit /b 1
)

REM 启动Chrome调试模式
echo 启动Chrome调试模式...
echo 调试端口: 9222
echo 用户数据目录: D:\anr-analyzer\chrome-debug-profile
echo Chrome路径: %CHROME_PATH%
echo.

start "" %CHROME_PATH% ^
    --remote-debugging-port=9222 ^
    --user-data-dir="D:\anr-analyzer\chrome-debug-profile" ^
    --no-first-run ^
    --no-default-browser-check ^
    --disable-blink-features=AutomationControlled ^
    --disable-extensions ^
    --disable-plugins ^
    --disable-web-security ^
    --allow-running-insecure-content ^
    --disable-features=VizDisplayCompositor ^
    --disable-dev-shm-usage ^
    --no-sandbox ^
    --disable-gpu ^
    --window-size=1920,1080 ^
    --disable-sync ^
    --disable-background-networking ^
    --disable-background-timer-throttling ^
    --disable-renderer-backgrounding ^
    --disable-backgrounding-occluded-windows ^
    --disable-client-side-phishing-detection ^
    --disable-default-apps ^
    --disable-hang-monitor ^
    --disable-prompt-on-repost ^
    --disable-domain-reliability ^
    --disable-component-extensions-with-background-pages ^
    --disable-ipc-flooding-protection ^
    --no-default-browser-check ^
    --no-first-run ^
    --disable-logging ^
    --disable-gpu-logging ^
    --silent-debugger-extension-api ^
    --disable-component-update ^
    --disable-background-downloads ^
    --disable-add-to-shelf ^
    --disable-client-side-phishing-detection ^
    --disable-datasaver-prompt ^
    --disable-desktop-notifications ^
    --disable-domain-reliability ^
    --disable-features=TranslateUI ^
    --disable-ipc-flooding-protection ^
    --disable-prompt-on-repost ^
    --disable-sync-preferences ^
    --disable-web-resources ^
    --disable-background-networking ^
    --disable-background-timer-throttling ^
    --disable-renderer-backgrounding ^
    --disable-backgrounding-occluded-windows ^
    --disable-component-extensions-with-background-pages ^
    --disable-default-apps ^
    --disable-hang-monitor ^
    --disable-logging ^
    --disable-gpu-logging ^
    --silent-debugger-extension-api ^
    --disable-component-update ^
    --disable-background-downloads ^
    --disable-add-to-shelf ^
    --disable-client-side-phishing-detection ^
    --disable-datasaver-prompt ^
    --disable-desktop-notifications ^
    --disable-domain-reliability ^
    --disable-features=TranslateUI ^
    --disable-ipc-flooding-protection ^
    --disable-prompt-on-repost ^
    --disable-sync-preferences ^
    --disable-web-resources

REM 等待Chrome启动
echo 等待Chrome启动...
timeout /t 5 /nobreak >nul

REM 检查调试端口是否启动
echo 检查调试端口状态...
netstat -an | findstr :9222
if %errorlevel% equ 0 (
    echo.
    echo ✅ Chrome调试模式启动成功！
    echo ✅ 调试端口9222正在监听
    echo ✅ 这是一个无谷歌账号的Chrome实例
    echo ✅ IDEA程序现在可以连接到这个Chrome实例
    echo.
    echo 现在可以启动IDEA程序，程序会自动访问IoT平台
    echo IoT平台地址: https://iot-admin.zuoyebang.cc/iot-admin-server/index
    echo.
) else (
    echo.
    echo ❌ Chrome调试模式启动失败！
    echo ❌ 调试端口9222未启动
    echo.
    echo 请检查：
    echo 1. Chrome是否正常启动
    echo 2. 端口9222是否被其他程序占用
    echo 3. 防火墙是否阻止了端口访问
    echo 4. Chrome路径是否正确
    echo.
)

echo 按任意键退出...
pause >nul
